package br.pucrs.lognplay.service;

import java.util.List;

import javax.ejb.Stateless;

import br.pucrs.lognplay.entity.StaticView;
import br.pucrs.lognplay.repository.RepositoryManager;
import br.pucrs.lognplay.repository.StaticViewRepository;

@Stateless
public class StaticViewManagementServiceBean implements StaticViewManagementService {

	public void mergeStaticView(StaticView staticView) throws ServiceException {
		try {
			RepositoryManager.beginTransaction();
			StaticViewRepository staticViewRepository = RepositoryManager.getStaticViewRepository();
			staticViewRepository.merge(staticView);
			RepositoryManager.commitTransaction();
		} catch (Exception e) {
			RepositoryManager.rollbackTransaction();
			throw new ServiceException("Error on merge a static view", e);
		}
	}

	public StaticView getStaticViewById(String id) throws ServiceException {
		try {
			RepositoryManager.beginTransaction();
			StaticViewRepository staticViewRepository = RepositoryManager.getStaticViewRepository();
			StaticView staticView = staticViewRepository.get(id);
			RepositoryManager.commitTransaction();
			return staticView;
		} catch (Exception e) {
			RepositoryManager.rollbackTransaction();
			throw new ServiceException("Error on get a static view by id", e);
		}
	}

	public List<StaticView> listAllStaticViews() throws ServiceException {
		try {
			RepositoryManager.beginTransaction();
			StaticViewRepository staticViewRepository = RepositoryManager.getStaticViewRepository();
			List<StaticView> staticViews = staticViewRepository.listAll();
			RepositoryManager.commitTransaction();
			return staticViews;
		} catch (Exception e) {
			RepositoryManager.rollbackTransaction();
			throw new ServiceException("Error on list all static views", e);
		}
	}

	public List<StaticView> listStaticViewsByClientId(String clientId) throws ServiceException {
		try {
			RepositoryManager.beginTransaction();
			StaticViewRepository staticViewRepository = RepositoryManager.getStaticViewRepository();
			List<StaticView> staticViews = staticViewRepository.listByClientId(clientId);
			RepositoryManager.commitTransaction();
			return staticViews;
		} catch (Exception e) {
			RepositoryManager.rollbackTransaction();
			throw new ServiceException("Error on list static views by client id", e);
		}
	}
}
